<?php

class AdminTagRelController extends BaseController {

    private $rules = array(
        'nombre' => 'required',
    );

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $tagrels = TagRel::getTagSinAnonima()->get();
        return View::make('admin.tagrels.index', ['tagrels' => $tagrels]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('admin.tagrels.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        // validate
        $validator = Validator::make(Input::all(), $this->rules);
        
        // process the form
        if ($validator->fails()) {
              return Redirect::back()->withErrors($validator)->withInput();
        } else {
            // crea el slug a partir del nombre
            $nombre = Input::get('nombre');
            $slug = Str::slug($nombre, $separator = '-');
            // store
            $tagrel = new TagRel;
            $tagrel->nombre = $nombre;
            $tagrel->slug = $slug;
            $tagrel->save();
            // redirect
            Session::flash('message', $nombre . ' fue creado');
            return Redirect::to('admin/tagrels');
        }        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
//    public function show($id) {
//    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $tagrel = TagRel::find($id);
        return View::make('admin.tagrels.edit', ['tagrel' => $tagrel]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $validator = Validator::make(Input::all(), $this->rules);

        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)->withInput(Input::except('password'));
        } 
        else {
            // crea el slug a partir del nombre
            $nombre = Input::get('nombre');
            $slug = Str::slug($nombre, $separator = '-');
            // store
            $tagrel = TagRel::find($id);
            $tagrel->nombre = $nombre;
            $tagrel->slug = $slug;
            $tagrel->save();
            // redirect
            Session::flash('message', $nombre . ' fue modificado.');
            return Redirect::to('admin/tagrels');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        // delete
        $tagrel = TagRel::find($id);
        
        DB::transaction(function($tagrel) use ($tagrel) {
            // cambiar a categ ANONIMA #1 a los relatos relacionados
            DB::table('relato_tag_rel')
                    ->where('tag_rel_id', $tagrel->id)
                    ->update(['tag_rel_id' => 1]);
            // Borrar tag
            $tagrel->delete();
        });

        // redirect
        Session::flash('message', 'Eliminación exitosa');
        return Redirect::to('admin/tagrels');
    }
}